package com.uzai.console.web.log;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.console.dto.log.useroperationlog.UserOperationLogQuery;
import com.uzai.console.dto.uzaichat.kefuuser.KefuUserQuery;
import com.uzai.console.entity.KefuUser;
import com.uzai.console.entity.Merchant;
import com.uzai.console.entity.es.EsUserOperationLog;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.mapper.KefuUserMapper;
import com.uzai.console.mapper.MerchantMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.log.UserOperationLogService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 操作日志
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "操作日志",tags = "查看日志|操作日志")
@ApiSort(26)
@RestController
public class UserOperationLogController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(UserOperationLogController.class);

    @Autowired
    private UserOperationLogService userOperationLogService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private KefuUserMapper kefuUserMapper;

    @ApiOperation(value = "查询账号操作日志列表", notes = "查询账号操作日志列表", response = EsUserOperationLog.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/userOperationLog/findUserOperationLogList")
    public Object findUserOperationLogList(@RequestBody UserOperationLogQuery userOperationLogQuery){
        return userOperationLogService.findUserOperationLogList(userOperationLogQuery);
    }

    @ApiOperation(value = "查询账号下拉列表", notes = "查询账号列表", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 2)
    @PostMapping("/userOperationLog/findAccountList")
    public Object findAccountList(){
        List<SelectKeyVo> selectKeyVoList = new ArrayList<>();
        //先封装主账号
        Merchant merchant = merchantMapper.selectById(getLoginInfo().getMerId());
        SelectKeyVo selectKeyVo = new SelectKeyVo();
        selectKeyVo.setKey(merchant.getMobile());
        selectKeyVo.setValue(merchant.getName());
        selectKeyVoList.add(selectKeyVo);

        //在查询子账号
        KefuUserQuery kefuUserQuery = new KefuUserQuery();
        kefuUserQuery.setMerId(getLoginInfo().getMerId());
        List<KefuUser> userList = kefuUserMapper.findByList(kefuUserQuery);
        if(userList != null && userList.size() > 0){
            for(KefuUser kefuUser : userList){
                SelectKeyVo selectKeyVo_kefu = new SelectKeyVo();
                selectKeyVo_kefu.setKey(kefuUser.getUser());
                selectKeyVo_kefu.setValue(kefuUser.getName());
                selectKeyVoList.add(selectKeyVo_kefu);
            }
        }
        return selectKeyVoList;
    }

    @ApiOperation(value = "查询操作模块下拉列表", notes = "查询操作模块下拉列表", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 3)
    @PostMapping("/userOperationLog/findModelList")
    public Object findModelList(){
        List<SelectKeyVo> selectKeyVoList = new ArrayList<>();
        //先封装主账号
        SystemModelEnum[] systemModelEnumArr = SystemModelEnum.values();
        if(systemModelEnumArr != null){
            for (SystemModelEnum systemModelEnum : systemModelEnumArr){
                SelectKeyVo selectKeyVo = new SelectKeyVo();
                selectKeyVo.setKey(systemModelEnum.getKey());
                selectKeyVo.setValue(systemModelEnum.getDesc());
                selectKeyVoList.add(selectKeyVo);
            }
        }
        return selectKeyVoList;
    }

    @ApiOperation(value = "查询操作类型下拉列表", notes = "查询操作类型下拉列表", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 4)
    @PostMapping("/userOperationLog/findTypelList")
    public Object findTypelList(){
        List<SelectKeyVo> selectKeyVoList = new ArrayList<>();
        //先封装主账号
        OperationTypeEnum[] operationTypeEnumArr = OperationTypeEnum.values();
        if(operationTypeEnumArr != null){
            for (OperationTypeEnum operationTypeEnum : operationTypeEnumArr){
                SelectKeyVo selectKeyVo = new SelectKeyVo();
                selectKeyVo.setKey(operationTypeEnum.getKey());
                selectKeyVo.setValue(operationTypeEnum.getDesc());
                selectKeyVoList.add(selectKeyVo);
            }
        }
        return selectKeyVoList;
    }


}
